package com.example.orders.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.orders.entity.Orders;
import com.example.orders.entity.User;
import com.example.orders.entity.vo.R;
import com.example.orders.feign.MyOpenFeign;
import com.example.orders.mapper.OrdersMapper;
import com.example.orders.service.OrdersService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * (Orders)表服务实现类
 *
 * @author makejava
 * @since 2025-07-24 10:19:10
 */
@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private MyOpenFeign myOpenFeign;


    //需求：查询订单对象的同时，查询所属的用户对象，封装到 Orders对象中，一起返回
    @Override
    public Object myGetById(Serializable id) {
        /*
            1: 利用mybatisplus原始的方法,完成数据库的查询
            2: 根据订单对象中的  userId字段的值 远程调用 MyUser 服务中的  controller类中的 selectOne 方法,并将返回的结果 自行处理!!!
            3: 将处理后的结果 存入 Orders 对象中即可
            4: 返回 Orders 对象
         */
        // 1: 利用mybatisplus原始的方法,完成数据库的查询
        Orders byId = getById(id);
        // HttpClient    RestTemplate   OpenFeign
        // 由于被调用 的 myuser服务中的 controller层,要求发送get请求,且携带路径参数,在这里需要按照要求调用
        /*R object = restTemplate.getForObject("http://localhost:9001/user/" + byId.getUserId(), R.class);
        User user = new User();
        try {
            BeanUtils.populate((Object) user, (Map<String, ? extends Object>) object.getData());
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }*/
        User user = myOpenFeign.selectOne(byId.getUserId());
        /*R object = myOpenFeign.selectOne(byId.getUserId());
        User user = new User();
        try {
            BeanUtils.populate((Object) user, (Map<String, ? extends Object>) object.getData());
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }*/
        byId.setUser(user);
        return byId;
    }

    @Override
    public Object myList() {
        /*
            1: 调用原始的list方法获取list
            2: 遍历list根据遍历到的每一个订单对象中的userId,远程调用查询对应的 User对象
            3: 将查询到的user对象存入order对象即可
         */
        List<Orders> list = list();
        for (Orders order : list) {
//            R object = restTemplate.getForObject("http://localhost:9001/user/" + order.getUserId(), R.class);
            User user = myOpenFeign.selectOne(order.getUserId());
            /*R object = myOpenFeign.selectOne(byId.getUserId());
            User user = new User();

            try {
                BeanUtils.populate((Object) user, (Map<String, ? extends Object>) object.getData());
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            } catch (InvocationTargetException e) {
                throw new RuntimeException(e);
            }*/
            order.setUser(user);
        }
        return list;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */

}
